<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/11/16 0016
 * Time: 15:10
 */

namespace app\admin\controller;
use app\admin\model\AuthRole;
use think\Controller;
use think\Db;

class AdminRole extends Controller
{
	/**
	 * 管理员列表主页
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
	public function index(){
		$authRole = AuthRole::paginate(10);
//		foreach ( $authRole as $k => $item ) {
//			$item = Db::name('auth_rule')->field('rule_name')->where('rule_id','in',$item)->select();
//			$rule_name = '';
//			foreach ($item as $name){
//				$rule_name .= $name['rule_name'].",";
//			}
//			$authRole[$k]->rule_name = $rule_name ;
//		}
		$this->assign('authRole',$authRole);
		return $this->fetch();
	}

	/**
	 * 删除用户
	 */
	public function del(){
		$role_id = input('role_id');
		$res = AuthRole::destroy($role_id);
		if ($res){
			$this->success('已删除');
		}else{
			$this->error('删除失败');
		}
	}


	/**
	 * 禁用启用管理员角色
	 */
	public function adminStatus(){
		$post = input('post.');
		if ($post['role_status']=='启用'){
			$ok = AuthRole::where('role_id',$post['role_id'])->update(['role_status'=>0]);
			if ($ok){
				$this->success('已启用');
			}else{
				$this->error('启用失败，请稍后再试');
			}
		}else{
			$ok= AuthRole::where('role_id',$post['role_id'])->update(['role_status'=>1]);
			if ($ok){
				$this->success('已停用');
			}else{
				$this->error('停用失败，请稍后再试');
			}
		}
	}


	public function edit(){
		$role_id = input('role_id');
		$authRole = AuthRole::get($role_id);
		$rule_id = ($authRole->rule_id);
		$this->assign('authRole',$authRole);
		$cate = Db::name('auth_rule_cate')->select();
		$this->assign('cate',$cate);
		$ruleCate = Db::name('auth_rule')
			->alias('t1')
			->join('auth_rule_cate t2','t1.rule_cate_id = t2.rule_cate_id')
			->where('t1.rule_id','in',$rule_id)
			->select();
//		$a = [];
//		foreach ( $ruleCate as $k=>$item ) {
//			var_dump($a[$item['rule_cate
//_name']][]);
//		}
		return $this->fetch();
	}
}